package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class UhrpAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.uhrp.org/article/[0-9]{9,}",
		"http://chineseblog.uhrp.org/\\?p=[0-9]*",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		if (title.contains("|")){
			title=StringUtils.substringBefore(title, "|");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select(".content"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".entry-content"));
//		if (date==null)date =JSoupUtils.matchDate(doc, "日期：");
		if (date==null)date =DateUtils.matchDate(doc.select(".node-date").text());
		if (date==null){
			String dateStr=doc.select(".entry-date").text();
			if(dateStr!=null){
				dateStr=dateStr.replace("一", "1");
				dateStr=dateStr.replace("二", "2");
				dateStr=dateStr.replace("三", "3");
				dateStr=dateStr.replace("四", "4");
				dateStr=dateStr.replace("五", "5");
				dateStr=dateStr.replace("六", "6");
				dateStr=dateStr.replace("七", "7");
				dateStr=dateStr.replace("八", "8");
				dateStr=dateStr.replace("九", "9");
				dateStr=dateStr.replace("十", "10");
				dateStr=dateStr.replace("十一", "11");
				dateStr=dateStr.replace("十二", "12");
				date =DateUtils.matchDate(dateStr);
			}
		}
//		author=JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isBlank(author))author=doc.select("a.article-link").text();
		if(StringUtils.isBlank(author))author=doc.select(".author").text();
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
